// TankMsg.java
package com.tankgame.server;

import com.tankgame.models.BaseTank;
import com.tankgame.models.PlayerTank;
import com.tankgame.utils.Directions;
import com.tankgame.utils.UniqueTankVector;

import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

public class TankMsg {
  private UniqueTankVector tanks = new UniqueTankVector();
  private BaseTank tank;

  public TankMsg(UniqueTankVector tanks) {
    this.tanks = tanks;
  }

  public TankMsg(BaseTank tank) {
    this.tank = tank;
  }

  public TankMsg() {}


  //发送消息方法
  public void sendTank(DatagramSocket ds, String ip, int udpPort) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    System.out.println("client 发送单个坦克" + tank.getId());
    try {
      dos.writeInt(tank.getId());
      dos.writeInt(tank.getX());
      dos.writeInt(tank.getY());
      dos.writeInt(tank.getDirection().ordinal());
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    byte[] buf = baos.toByteArray();
    DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress(ip, udpPort));
    try {
      ds.send(dp);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  public void sendAllTank(DatagramSocket ds, String ip, int udpPort) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    System.out.println("server 发送所有tank" + tanks.size());
    try {
      dos.writeInt(tanks.size());
      for (BaseTank t : tanks) {
        dos.writeInt(t.getId());
        dos.writeInt(t.getX());
        dos.writeInt(t.getY());
        dos.writeInt(t.getDirection().ordinal());
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    byte[] buf = baos.toByteArray();
    DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress(ip, udpPort));
    try {
      ds.send(dp);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  public BaseTank parseTank(DataInputStream dis) {
    try {
      int id = dis.readInt();
      System.out.println("服务器解析到单个坦克" + id);
      int x = dis.readInt();
      int y = dis.readInt();
      Directions direction = Directions.values()[dis.readInt()];
      tank = new PlayerTank(x, y);
      tank.setId(id);
      tank.setDirection(direction);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return tank;
  }

  public UniqueTankVector parseAllTank(DataInputStream dis) {

    UniqueTankVector tanks = new UniqueTankVector();
    try {
      int size = dis.readInt();
      System.out.println("客户端解析到多个坦克" + size);
      for (int i = 0; i < size; i++) {
        int id = dis.readInt();
        int x = dis.readInt();
        int y = dis.readInt();
        Directions direction = Directions.values()[dis.readInt()];
        PlayerTank t = new PlayerTank(x, y);
        t.setId(id);
        t.setDirection(direction);
        tanks.add(t);
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return tanks;
  }
}


